<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;
use Mockery\Matcher\Type;

class Types extends Model
{
    protected $table = 'types';

    /**
     * @return mixed
     * @name 分类数据
     */
    public static function getListData($data){
        $where=array();
        if(!empty($data['tid'])){
            $where[]=['tid','=',$data['tid']];
        }else{
            $where[]=['tid','!=',0];
        }
        if(!empty($data['keyword'])){
            $where[]=['name','like',"%".$data['keyword']."%"];
        }
        return DB::table('types')
            ->where($where)
            ->orderBy('weight','desc')
            ->orderBy('id','desc')
            ->paginate(15);
    }

    /**
     * @return mixed
     * @name 获取所有数据
     * @author wudean
     */
    public static function getAllData(){
        return DB::table('types')->orderBy('weight','desc')->orderBy('id','desc')->get();
    }

    /**
     * @param $id
     * @return mixed
     * @name 通过id获取一条信息
     */
    public static function getInfoById($id){
        return DB::table('types')->where('id',$id)->first();
    }

    /**
     * @name 获取导航数据分类
     * @return mixed
     * @author Deann.wu
     */
    public static function getListTopData($top_id=0){
       $lists=DB::table('types')
            ->where('tid',$top_id)
            ->orderBy('weight','desc')
            ->orderBy('id','desc')
            ->get();
       foreach ($lists as $list){
            $list->son=null;
            $son=self::getDataBySonList($list->id);
            if(!empty($son)){
                $list->son=$son;
            }
       }
       return $lists;
    }

    /**
     * @param $id
     * @name 子分页
     * @author Deann.wu
     */
    public static function getDataBySonList($id){
        return DB::table('types')
            ->where('tid',$id)
            ->orderBy('weight','desc')
            ->orderBy('id','desc')
            ->get();
    }
}
